import requests
import time
import requests
from bs4 import BeautifulSoup
import re
from ctypes import *
from PIL import ImageFont, ImageDraw, Image


s=[]

###pic
flag=0
radius1=0
timeOut=0
groupID=0
modeFlag=0
jiaodu=0
memberID=0
###malody
R1=11
G1=11
B1=11
flagMalody=1
title=0
score=0
acc=0
time1=0
combo=0
judge=0
s1=0

def ma(maid,cookie):
    global name,title,score,acc,time1,combo,judge,R1,G1,B1
    # maid=input("malodyID:")
    urlsearch="http://m.mugzone.net/page/search?keyword="+str(maid)
    r1 = requests.get(urlsearch,headers = {
        'Cookie':cookie,
        'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
        })
    html1 = r1.text
    soup1= BeautifulSoup(html1,"lxml")
    id1=soup1.find_all('a',{'class':'textfix'})
    id2=str(id1)
    l=7+len(maid)
    id3=id2[41:-l]
    id3=id3[0:len(maid)+1]
    usrid=id3   
    #print(usrid)
    url='http://m.mugzone.net/accounts/user/'+str(usrid)
    #print(url)
    r = requests.get(url,headers = {
        'Cookie':cookie,
        'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
        })
    html=r.text
    soup = BeautifulSoup(html,"lxml")
    for divv in soup.find_all('div',{'id':'center'}):
        for diva in soup.find_all('div',{'class':'user_head g_rblock'}):
            for divb in diva.find_all('div',{'class':'right'}):
                for divc in divb.find_all('p',{'class':'name'}):
                    for divd in divc.find_all('span'):
                        name=divd.string
    div = soup.find_all('div',{'class':'item g_rblock'})
    a1=list(div)[0].find('a')
    title=a1.string
    em=list(div)[0].find('em')
    judge=em.string
    p=list(div)[0].findAll('p')
    p1=list(p)[1]
    infos=str(p1)[3:-38]
    score=str(infos.split(':',3)[1])[1:-5]
    combo=str(infos.split(':',3)[2])[1:-5]
    acc=str(infos.split(':',3)[3])[0:]
    span=list(div)[0].find('span',{'class':'time'})
    time1=span.string
    div3= soup.find_all('div',{'class':'gb_profile'})
    b = list(div3)[0]
    pattern = re.compile(r'http://[\d\w\.\/\-\?\=\!\?]+')
    url = re.findall(pattern, str(b))
    img = requests.get(url[0])
    with open('./data/malody/b.png', 'wb') as f:
        f.write(img.content)
    a = list(div)[0]
    pattern = re.compile(r'http://[\d\w\.\/\-\?\=\!\?]+')
    url = re.findall(pattern, str(a))
    img = requests.get(url[0])
    with open('./data/malody/a.png', 'wb') as f:
        f.write(img.content)##########图片缩放142px
    imagepath = r'./data/malody/a.png'
    image = Image.open(imagepath)
    new_im = image.resize((142, 142), Image.Resampling.LANCZOS)
    new_im.save('./data/malody/a.png')
    imagepath = r'./data/malody/b.png'
    image = Image.open(imagepath)
    new_im = image.resize((142, 142), Image.Resampling.LANCZOS)
    new_im.save('./data/malody/b.png')#######文字合成
    font = ImageFont.truetype("./data/malody/NotoSansJP-Medium.ttf",30)
    font2 = ImageFont.truetype("./data/malody/NotoSansJP-Medium.ttf",160)
    im = Image.open("./data/malody/ma.png")
    draw = ImageDraw.Draw(im)  
    draw.text((225, 20),  str(name).strip(), font = font, fill = (255, 255, 255))
    if len(str(title))>20:
        title1=str(title)[:20]
        draw.text((250, 510),  str(title1).strip(), font = font, fill = (255, 255, 255))
        title2=str(title)[20:40]
        draw.text((250, 545),  str(title2).strip(), font = font, fill = (255, 255, 255))
        title3=str(title)[40:]
        draw.text((250, 580),  str(title3).strip(), font = font, fill = (255, 255, 255))
    else:
        draw.text((260, 540),  str(title).strip(), font = font, fill = (255, 255, 255))
    draw.text((860, 144), str(score).strip(), font = font, fill = (255, 255, 255))
    draw.text((1077, 144),  str(acc).strip(), font = font, fill = (255, 255, 255))
    draw.text((225, 75),  str(time1).strip(), font = font, fill = (255, 255, 255))
    draw.text((995, 500),  str(combo).strip(), font = font, fill = (255, 255, 255))
    if judge=='A':
        R1=103
        G1=240
        B1=171
    elif judge=='B':
        R1=217
        G1=240
        B1=103
    elif judge=='C':
        R1=240
        G1=194
        B1=103
    elif judge=='D':
        R1=228
        G1=145
        B1=103
    elif judge=='E':
        R1=239
        G1=102
        B1=102
    draw.text((480, 230),  str(judge), font = font2, fill = (R1,G1,B1))
    im.save('./data/cache/gen/malody.png')
    Picture_Synthesis('./data/cache/gen/malody.png',
                      './data/malody/a.png',
                      './data/cache/gen/malody.png',
                      coordinate=(97,495))
    Picture_Synthesis('./data/cache/gen/malody.png',
                      './data/malody/b.png',
                      './data/cache/send/malody.png',
                      coordinate=(16,16))
    return(name,title,score,acc,time1,combo,judge)


def Picture_Synthesis(mother_img,
                      son_img,
                      save_img,
                      coordinate=None):
    from PIL import ImageFont, ImageDraw, Image
    M_Img = Image.open(mother_img)
    S_Img = Image.open(son_img)
    factor = 1
    M_Img = M_Img.convert("RGBA")
    M_Img_w, M_Img_h = M_Img.size 
    S_Img_w, S_Img_h = S_Img.size
    size_w = int(S_Img_w / factor)
    size_h = int(S_Img_h / factor)
    if S_Img_w > size_w:
        S_Img_w = size_w
    if S_Img_h > size_h:
        S_Img_h = size_h
    icon = S_Img.resize((S_Img_w, S_Img_h), Image.Resampling.LANCZOS)
    w = int((M_Img_w - S_Img_w) / 2)
    h = int((M_Img_h - S_Img_h) / 2)
    try:
        if coordinate==None or coordinate=="":
            coordinate=(w, h)
            M_Img.paste(icon, coordinate, mask=None)
        else:
            M_Img.paste(icon, coordinate, mask=None)
    except:
        pass
    M_Img.save(save_img)